package cn.com.thingo.hyintelligent.ui.activity;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.MessageContract;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.MessageListBean;
import cn.com.thingo.hyintelligent.presenter.MessagePresenter;
import cn.com.thingo.hyintelligent.ui.adapter.MessageListAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.hyintelligent.util.SPUtils;

public class MessageActivity extends BaseActivity<MessagePresenter> implements MessageContract.View, MessageListAdapter.OnItemClickListener {
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.message_list)
    RecyclerView rvList;
    private MessageListAdapter adapter;
    private List<MessageListBean.DataBean> list = new ArrayList<>();

    @Override
    protected void initEventAndData() {
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mPresenter.messageList(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN));
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.system_message);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter = new MessageListAdapter(list, mContext);
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(int position) {
        mPresenter.readMessage(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), list.get(position).getId(), position);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_message;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void viewMessageList(MessageListBean messageListBean) {
        List<MessageListBean.DataBean> data = messageListBean.getData();
        list.clear();
        if (data != null && data.size() > 0) {
            list.addAll(data);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void viewReadMessage(CommonBean commonBean, int position) {
        MessageListBean.DataBean dataBean = list.get(position);
        Intent intent = new Intent(mContext, MessageDetailActivity.class);
        intent.putExtra("intent_message_title", dataBean.getTitle());
        intent.putExtra("intent_message_content", dataBean.getContent());
        intent.putExtra("intent_message_create_time", dataBean.getCreate_time());
        startActivity(intent);
    }
}
